package demo.sicau.votesystem.service.imp;

import demo.sicau.votesystem.constant.DataConstant;
import demo.sicau.votesystem.dao.AdminDao;
import demo.sicau.votesystem.dao.UserDao;
import demo.sicau.votesystem.entity.PO.AdminPO;
import demo.sicau.votesystem.entity.PO.UserPO;
import demo.sicau.votesystem.entity.VO.ResultVO;
import demo.sicau.votesystem.service.AdminService;
import demo.sicau.votesystem.service.UserService;
import demo.sicau.votesystem.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AdminServiceImp implements AdminService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private ResultVOUtil resultVOUtil;


    @Override
    public ResultVO adminLogin(String admin_name, String password, HttpServletRequest request){
        if("".equals(admin_name.trim())&&"".equals(password.trim())){
            return resultVOUtil.paramError();
        }else {
            try {
                AdminPO adminPO=adminDao.selectAdminByPwdAndUsername(admin_name,password,request);
                if(adminPO==null){
                    return new ResultVOUtil().loginError();
                }else {
                    HttpSession session=request.getSession();
                    session.setAttribute(DataConstant.ADMIN_SESSION_NAME,adminPO.getId());
                    List<UserPO> userPOS=userDao.selectUsers();
                    return resultVOUtil.success("登陆成功",userPOS);
                }

            }catch (Exception e){
                e.printStackTrace();
                return resultVOUtil.unknowError();
            }
        }
    }
}
